<template>
  <el-dialog v-model="visible" :title="!dataForm.thyId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="联络记录id" prop="lrdId">
        <el-input v-model="dataForm.lrdId" placeholder="联络记录id"></el-input>
      </el-form-item>
      <el-form-item label="员工id" prop="stfId">
        <el-input v-model="dataForm.stfId" placeholder="员工id"></el-input>
      </el-form-item>
      <el-form-item label="员工结果" prop="stfName">
        <el-input v-model="dataForm.stfName" placeholder="员工结果"></el-input>
      </el-form-item>
      <el-form-item label="呼叫结果" prop="thyResult">
        <el-input v-model="dataForm.thyResult" placeholder="呼叫结果"></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyReason">
        <el-input v-model="dataForm.thyReason" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyLinkTime">
        <el-input v-model="dataForm.thyLinkTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyWizardHis">
        <el-input v-model="dataForm.thyWizardHis" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyRecordFile">
        <el-input v-model="dataForm.thyRecordFile" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyIfLast">
        <el-input v-model="dataForm.thyIfLast" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyLinkNum">
        <el-input v-model="dataForm.thyLinkNum" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyTimeSpan">
        <el-input v-model="dataForm.thyTimeSpan" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyOnHookType">
        <el-input v-model="dataForm.thyOnHookType" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyDtmf">
        <el-input v-model="dataForm.thyDtmf" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="观众id" prop="psnId">
        <el-input v-model="dataForm.psnId" placeholder="观众id"></el-input>
      </el-form-item>
      <el-form-item label="" prop="tskId">
        <el-input v-model="dataForm.tskId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyNextLinkTime">
        <el-input v-model="dataForm.thyNextLinkTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="exhId">
        <el-input v-model="dataForm.exhId" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thySaleIndex">
        <el-input v-model="dataForm.thySaleIndex" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyLinkContent">
        <el-input v-model="dataForm.thyLinkContent" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyTargetSize">
        <el-input v-model="dataForm.thyTargetSize" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thrTargetRevenue">
        <el-input v-model="dataForm.thrTargetRevenue" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyLinkType">
        <el-input v-model="dataForm.thyLinkType" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="" prop="thyCreateTime">
        <el-input v-model="dataForm.thyCreateTime" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  thyId: "",
  lrdId: "",
  stfId: "",
  stfName: "",
  thyResult: "",
  thyReason: "",
  thyLinkTime: "",
  thyWizardHis: "",
  thyRecordFile: "",
  thyIfLast: "",
  thyLinkNum: "",
  thyTimeSpan: "",
  thyOnHookType: "",
  thyDtmf: "",
  psnId: "",
  tskId: "",
  thyNextLinkTime: "",
  exhId: "",
  thySaleIndex: "",
  thyLinkContent: "",
  thyTargetSize: "",
  thrTargetRevenue: "",
  thyLinkType: "",
  thyCreateTime: "",
  creator: "",
  createTime: "",
  updater: "",
  updateTime: "",
});

const rules = ref({
});

const init = (thyId?: number) => {
  visible.value = true;
  dataForm.thyId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (thyId) {
    getInfo(thyId);
  }
};

// 获取信息
const getInfo = (thyId: number) => {
  baseService.get("/crm/crmtelhistory/" + thyId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.thyId ? baseService.post : baseService.put)("/crm/crmtelhistory", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>